package com.atguigu.serviceedu.controller;


import com.alibaba.excel.EasyExcel;
import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.SubjectData;
import com.atguigu.serviceedu.entity.subjectvo.OneSubjectVo;
import com.atguigu.commonutils.exception.GuliException;
import com.atguigu.serviceedu.handler.excel.SubjectExcelListener;
import com.atguigu.serviceedu.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author xuan
 * @since 2020-04-12
 */
//@CrossOrigin
@RestController
@RequestMapping("/edu/subject")
public class EduSubjectController {
    @Autowired
    private EduSubjectService subjectService;

    //添加课程分类，获取上传过来文件，把文件内容读取出来
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
            return R.ok();
        }catch(Exception e){
            throw new GuliException(20001,"添加课程分类失败");
        }
    }

    @GetMapping("findSubjectVos")
    public R getAllSubject(){
        List<OneSubjectVo> oneSubjectVos = subjectService.findSubjectVos();
        return R.ok().data("items",oneSubjectVos);
    }

}

